package com.blog.wl2023.controller.com;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.blog.wl2023.constant.Code;
import com.blog.wl2023.entity.response.Result;
import com.blog.wl2023.exception.CustomException;
import com.blog.wl2023.service.user.UserService;
import com.blog.wl2023.utils.CommUtil;
import com.blog.wl2023.utils.GiteeImgUtil;
import com.blog.wl2023.utils.QiniuUtil;
import com.blog.wl2023.utils.note.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author wule
 * @desc 七牛云图片上传接口
 */
@Slf4j
@RestController
@ResponseResult
@RequestMapping("api/qiniu")
public class QiniuController {

	@Autowired
	private UserService userService;


	/**
	 * 七牛云文件上传
	 * 接受单个视频
	 * @param video 视频文件
	 * @returnd 给前端返回一个文件访问地址
	 */
	@PostMapping("/article/uploadVideo")
	public Result uploadVideo(@RequestParam(value = "video") MultipartFile  video,
	                     @RequestParam(value = "fileName") String fileName) {
		// 验证文件类型
		List<String> allowedExtensions = List.of("mp4");
		String fileExtension = StringUtils.getFilenameExtension(video.getOriginalFilename());
		if (fileExtension != null && !allowedExtensions.contains(fileExtension.toLowerCase())) {
			return Result.operateFail("只允许上传mp4格式的视频文件");
		}
		try {
			//定义文件的名字(前端)
			InputStream inputStream = video.getInputStream();
			fileName = "articleVideos/" + fileName;
			log.info("上传视频名为:" + fileName);
			QiniuUtil qiniuUtil = new QiniuUtil();
			qiniuUtil.upload(inputStream, fileName);
			return Result.ok(fileName);
		} catch (IOException e) {
			throw new CustomException(Code.OPERATE_FAIL);
		}
	}

	/**
	 * 接受博客的头图片   和    文章内插图,评论图片
	 *
	 * @param file     文件
	 * @param fileName 图片名字(已封装)
	 * @return 图片线上地址
	 */
	@PostMapping("/article/uploadArticleImage")
	public Result uploadArticleImage(@RequestParam("file") MultipartFile file,
	                                 @RequestParam("fileName") String fileName) {
		try {
			fileName = "articleImages/" + fileName;

			log.info("上传图片名为:" + fileName);

			InputStream inputStream = file.getInputStream();

			QiniuUtil qiniuUtil = new QiniuUtil();

			qiniuUtil.upload(inputStream, fileName);

			return Result.ok(fileName);

		} catch (IOException e) {
			throw new CustomException(Code.OPERATE_FAIL);
		}
	}


	/**
	 * 接受博客的文章插图
	 * @param file     文件
	 * @param fileName 图片名字(已封装)
	 * @return 图片线上地址
	 */
	@PostMapping("/article/giteeUploadArticleImage")
	public Result giteeUploadArticleImage(@RequestParam("file") MultipartFile file,
	                                 @RequestParam("fileName") String fileName) {
		try {
			if (file == null) {
				return Result.fail(201,"文件不能为空");
			}
			//控制上传的文件夹
			fileName = "articleImages/" + CommUtil.getYearMonth() + "/" + fileName;

			log.info("上传图片名为:" + fileName);

			//根据文件名生成指定的请求url
			String targetURL = GiteeImgUtil.createUploadFileUrl(fileName);

			//请求体封装
			Map<String, Object> uploadBodyMap = GiteeImgUtil.getUploadBodyMap(file.getBytes());

			//借助HttpUtil工具类发送POST请求
			String JSONResult = HttpUtil.post(targetURL, uploadBodyMap);


			//解析响应JSON字符串
			JSONObject jsonObj = JSONUtil.parseObj(JSONResult);

			//请求失败
			if (jsonObj.getObj("commit") == null) {
				return Result.fail(201,"上传失败");
			}

			//请求成功：返回下载地址
			JSONObject content = JSONUtil.parseObj(jsonObj.getObj("content"));
			Object download_url = content.getObj("download_url");
			return Result.ok(download_url);

		} catch (IOException e) {
			throw new CustomException(Code.OPERATE_FAIL);
		}
	}














	@PostMapping("/user/uploadUserAvatar")
	public Result uploadUserAvatar(@RequestParam("file") MultipartFile file,
	                                 @RequestParam("fileName") String fileName,
	                               @RequestParam("uuid") String uuid) {
		try {
			fileName = "userAvatars/" + fileName;

			log.info("上传图片名为:" + fileName);

			InputStream inputStream = file.getInputStream();

			QiniuUtil qiniuUtil = new QiniuUtil();

			qiniuUtil.upload(inputStream, fileName);

			//将图片信息存入数据库
			userService.updateUserAvatar(uuid,fileName);

			return Result.ok(fileName);

		} catch (IOException e) {
			throw new CustomException(Code.OPERATE_FAIL);
		}
	}
















}
